﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GrainManage.Data.Util;
using GrainManage.Data.Model.Table;
using GrainManage.Data.Model.View;
using GrainManage.Data.Model;

namespace GrainManage.Data.Service
{
    public class ImageService : BaseService
    {

        public ImageService(GrainManage.Data.Access.AccessBase proc, GrainManage.Data.Pattern.IPattern pattern) : base(proc, pattern) { }

        public byte[] GetFile(int imageID, bool withFile)
        {
            byte[] result = null;
            using (var reader = Proc.ExecuteReader(ProcConst.Image_GetFileByID, imageID, withFile))
            {
                if (reader.Read())
                {
                    object data = reader[0];
                    if (data != DBNull.Value)
                    {
                        result = data as byte[];
                    }
                }
            }
            return result;
        }

        public List<ImageTable> GetImages(bool withFile, IPageable page, ICountable records)
        {
            DataSet ds = Proc.ExecuteQuery(ProcConst.Image_GetAllByExtent, withFile, GetOffset(page), page.PageSize);
            return ConvertToModel<ImageTable>(ds, records);
        }

        public string GetImagePath(int imageID)
        {
            return Proc.ExecuteScalar(ProcConst.Image_GetPathByID, imageID) as string;
        }

        public string GetImageUrl(int imageID)
        {
            return Proc.ExecuteScalar(ProcConst.Image_GetUrlByID, imageID) as string;
        }

        public int GetImageID(string imageName, string creator)
        {
            return Convert.ToInt32(Proc.ExecuteScalar(ProcConst.Image_GetIDByName, imageName, creator));
        }

        public int Insert(ImageTable image)
        {
            Initializor.Initialize(image, string.Empty);
            var result = Proc.ExecuteScalar(ProcConst.Image_Insert, image.ImageName, image.PathID, image.ImageType, image.Remarks, image.Creator);
            return result == null ? 0 : Convert.ToInt32(result);
        }

        public int Update(ImageTable image)
        {
            Initializor.Initialize(image, string.Empty);
            return Proc.Execute(ProcConst.Image_UpdateByID, image.ImageID, image.ImageName, image.PathID, image.ImageType, image.Remarks, image.Creator);
        }

        public int Delete(int imageID)
        {
            return Proc.Execute(ProcConst.Image_DeleteByID, imageID);
        }

        public List<int> Insert(List<ImageTable> images)
        {
            var imageIDs = new List<int>();
            foreach (var image in images)
            {
                imageIDs.Add(Insert(image));
            }
            return imageIDs;
        }

        public List<int> Update(List<ImageTable> images)
        {
            var inValidImageIDs = new List<int>();
            foreach (var image in images)
            {
                if (Update(image) == 0)
                {
                    inValidImageIDs.Add(image.ImageID);
                }
            }
            return inValidImageIDs;
        }

        public List<int> Delete(List<int> images)
        {
            var inValidImages = new List<int>();
            foreach (var image in images)
            {
                if (Delete(image) == 0)
                {
                    inValidImages.Add(image);
                }
            }
            return inValidImages;
        }
    }
}
